package com.ffcs.test.sentinel;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 天真热
 * @create 2024-01-25 19:48
 * sentinel流控可以再客户端可视化界面实现，这里主要是为了实现代码流控
 * https://github.com/alibaba/Sentinel/wiki/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8
 * @desc
 **/
@RestController
@RequestMapping("/sentinel")
public class SentinelController_console {

    public final static String RESOURCE_NAME = "sentinel/console1";
    public final static String RESOURCE_NAME1 = "sentinel/consoleSleep1";
    //=================================控制台+流控====================================

    /**
     * 简单实现流控
     *
     * @return
     */
    @RequestMapping("/console1")
    @SentinelResource(value = RESOURCE_NAME, blockHandler = "blockHandlerMethod")
    public String console1() {
        return "测试";
    }

    public String blockHandlerMethod(BlockException blockException) {
        return "触发流控";
    }

    //=================================控制台+线程控====================================
    @RequestMapping("/consoleSleep1")
    @SentinelResource(value = RESOURCE_NAME1, blockHandler = "blockHandlerMethod1")
    public String consoleSleep1() throws InterruptedException {
        Thread.sleep(5000);
        return "测试";
    }

    public String blockHandlerMethod1(BlockException blockException) {
        return "触发线程控";
    }
}
